'use strict';
angular.module('accounting.accountCaptionSort', ['app.services'])

	.controller('accountCaptionSortCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, BizData, $uibModalInstance, params, title) {


   		var Model = this;
   		

   		/**
   		name:bizData#onBeforeDelete
   		description: <b>[回调型事件]</b>业务数据删除前
   		@param {object} event 它的结构如下:<br/>
   		{
   			"source":组件的js对象
   			,"cancel":可修改	，设置为true后中断当前delete动作
   			,"deleteIDList":删除的行Id数组
   		}
   		*/
   		Model.dataMasterBeforeDelete = function(event) {
//                               			alert(event.deleteIDList[0]);
//                               			event.cancel = true;
   		}
   		/**
   		name:bizData#onAfterDelete
   		description: <b>[回调型事件]</b>业务数据删除后
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   			"source" : 组件的js对象,
   			"deleteIDList" : 删除的行Id数组
   		}
   		</xmp>	
   		*/
   		Model.dataMasterAfterDelete = function(event) {
//                               			alert('删除之后！')
   		}

   		
   		
   		/**
   		name:bizData#onIndexChanging
   		description: <b>[回调型事件]</b>行记录变化中
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   			"source" : 组件的js对象,
   			"rowID" : 行Id,
   			"rowIndex" : 行索引,
   			"originalRowID" : 原行Id,
   			"originalRowIndex" : 原行索引
   		}
   		</xmp>	
   		*/
   		Model.dataMasterIndexChanging = function(event){
   		};
   		
   		
   		/**
   		name:bizData#onIndexChanged
   		description: <b>[回调型事件]</b>行记录变化
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   		"source" : 组件的js对象,
   		"rowID" : 行Id,
   		"rowIndex" : 行索引
   		}
   		</xmp>	
   		*/
   		Model.dataMasterIndexChanged = function(event) {
   		}
   		
   		/**
   		name:bizData#onDataChanged
   		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   		"source" : 组件的js对象,
   		"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
   		"selfChanged" : 是否自身变化，false：从data触发的变化,
   		"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
   		}
   		</xmp>	
   		*/
   		Model.dataMasterDataChanged = function(event) {
   		}
   		
   		/**
   		name:bizData#onValueChanged
   		description: <b>[回调型事件]</b>数据变化
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   		"source" : 组件的js对象,
   		"column" : 关系,
   		"rowIndex" : 行索引,
   		"value" : 新值,
   		"originalValue" : 旧值
   		}
   		</xmp>	
   		*/
   		//输入值变化，监视input的输入，ng-change
   		Model.dataMasterValueChanged = function(event) {
			
   		}
   		


   		/**
   		name:bizData#onBeforeNew
   		description: <b>[回调型事件]</b>业务数据新增前
   		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
   		*/
   		Model.dataMasterBeforeNew = function(event) {
			
   		};
   		
   		
   		/**
   		* name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
   		* 
   		* @param event
   		*            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
   		*/
   		Model.dataMasterAfterNew = function(event){

   		}
   		

   		
   		var options = {};
		options.container = Model;
		options.concept = 'AccountCaption';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
   		options.creater = '/accounting/accountCaption/tree/create';
   		options.reader = '/accounting/accountCaption/tree/query';
   		options.writer = '/accounting/accountCaption/tree/save';
   		options.deleter = '/accounting/accountCaption/tree/delete';
   		options.idColumnDefine = "fID";
   		options.idColumnName = "fID";
   		options.defCols = {fID:{name:"fID",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},billType:{name:"billType",type:"String",label:""},billTypeName:{name:"billTypeName",type:"String",label:""},orderDate:{name:"orderDate",type:"Date",label:""},state:{name:"state",type:"String",label:""},status:{name:"status",type:"String",label:""},captionNo:{name:"captionNo",type:"String",label:""},captionName:{name:"captionName",type:"String",label:""},captionFullID:{name:"captionFullID",type:"String",label:""},captionFullNo:{name:"captionFullNo",type:"String",label:""},captionFullName:{name:"captionFullName",type:"String",label:""},captionType:{name:"captionType",type:"String",label:""},captionTypeLabel:{name:"captionTypeLabel",type:"String",label:""},settleCategory:{name:"settleCategory",type:"String",label:""},settleCategoryLabel:{name:"settleCategoryLabel",type:"String",label:""},dob:{name:"dob",type:"Integer",label:""},dobLabel:{name:"dobLabel",type:"String",label:""},amountL:{name:"amountL",type:"Decimal",label:""},nodeLevel:{name:"nodeLevel",type:"Integer",label:""},nodeKind:{name:"nodeKind",type:"String",label:""},sortNo:{name:"sortNo",type:"String",label:""},fParent:{name:"fParent",type:"String",label:""},comments:{name:"comments",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},tUsercode:{name:"tUsercode",type:"String",label:""},tUsername:{name:"tUsername",type:"String",label:""},tDatetime:{name:"tDatetime",type:"Datetime",label:""},eUsercode:{name:"eUsercode",type:"String",label:""},eUsername:{name:"eUsername",type:"String",label:""},eDatetime:{name:"eDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
   		options.isTree = true;
   		options.parentRelation = 'fParent';
   		options.nodeKindRelation = 'nodeKind';
   		options.nodeLevelRelation = 'nodeLevel';
   		options.nodeSortRelation = "sortNo";
   		options.pathRelation = 'captionFullID';
   		options.directDeleteMode = false;
   		options.limit = -1;
   		options.orderBy = 'sortNo ASC';

   		options.onIndexChanging = Model.dataMasterIndexChanging;
   		options.onIndexChanged = Model.dataMasterIndexChanged;
   		options.onBeforeNew = null;
   		options.onAfterNew = Model.dataMasterAfterNew;
   		options.onBeforeDelete = null;
   		options.onAfterDelete = Model.dataMasterAfterDelete;
   		options.onValueChanged = Model.dataMasterValueChanged;
   		options.onDataChanged = Model.dataMasterDataChanged;
//      options.rowsWatcherFn = Model.rowsWatcherFn;
   		
   		Model.dataMaster = new BizData(options);
//      Model.dataMaster.refreshData();

//      Model.dataMaster.deregIndex = $scope.$watch('dataMaster.currentIndex',Model.dataMaster.watchCurrentIndex);
//      Model.dataMaster.deregID = $scope.$watch('dataMaster.currentID',Model.dataMaster.watchCurrentID);
   		//监视对象中的所有变动
   		Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
//      Model.dataMaster.deregCollection = $scope.$watch('dataMaster.rows',Model.dataMaster.watchCollection,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
   		//////////////////////////////////////////////


		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
            Model.title = title;  
            Model.params = params;  

            if(Model.params.parentID==null){
        		//设置过滤条件
        		Model.dataMaster.filter = "AccountCaption.fParent is null";//
            }else{
        		//设置过滤条件
        		Model.dataMaster.filter = "AccountCaption.fParent='"+ Model.params.parentID +"'";//
            }
      	
       		Model.dataMaster.refreshData();

        });  
		
        //置顶
        Model.btnMoveTopClick = function () {
        	var rowID = Model.dataMaster.getCurrentID();
        	var index = Model.dataMaster.getIndex(rowID);
        	if(rowID)
        	Model.dataMaster.moveRowPro(0 - index, rowID);
        }
        //上移
        Model.btnMoveUpClick = function () {
        	var rowID = Model.dataMaster.getCurrentID();
        	if(rowID)
        	Model.dataMaster.moveRowPro(-1, rowID);
        }
        //下移
        Model.btnMoveDownClick = function () {
        	var rowID = Model.dataMaster.getCurrentID();
        	if(rowID)
        	Model.dataMaster.moveRowPro(1, rowID);
        }
        //置底
        Model.btnMoveBottomClick = function () {
        	var rowID = Model.dataMaster.getCurrentID();
        	var index = Model.dataMaster.getIndex(rowID);
        	var count = Model.dataMaster.getTotal();
        	if(rowID)
        	Model.dataMaster.moveRowPro(count - index - 1, rowID);
        }
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
			for(var index=0;index<Model.dataMaster.rows.length;index++){
				var rowID = Model.dataMaster.rows[index].fID.value;
				data.push(rowID);
			}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
		//////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
